home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2277 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: 20 Jan 1996 05:02:22 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan19220222@qcd.lanl.gov>
  8. References: <4dorr8$i58@cloner3.netcom.com> <4dp2no$3va@unix.sri.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: mklenk@updike.sri.com's message of 19 Jan 1996 21:33:44 GMT
  13.  
  14. --text follows this line--
  15. In article <4dp2no$3va@unix.sri.com> mklenk@updike.sri.com (Mark
  16. Klenk) writes: 
  17. <snip>
  18.    >>
  19.    >>Is there a fast way to decide whether a number n is a power of 2?
  20.    >
  21.    >no tricks
  22.    >
  23.    >#define ISPOW2(x) (((x) & 1) ^ 1)
  24.  
  25.        He asked for POWER of 2, not MULTIPLE.
  26.  
  27.        Your macro will return non-zero for 6, for example,
  28.        which is not a power of 2.
  29.  
  30.        I don't think there is any way to do it other than
  31.        recursively or iteratively.
  32.  
  33. Don't think, try
  34.  
  35. #define POWER(x) (!((x) & ((x)-1)))
  36.  
  37. on unsigned types.
  38.  
  39. Cheers
  40. Tanmoy
  41.  
  42.  
  43.  
  44. --
  45. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  46. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  47. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  48. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  49. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  50. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  51.